package com.web.accumulate.controller;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import com.sun.deploy.net.URLEncoder;
import com.web.accumulate.entity.AccumulateTransaction;
import com.web.accumulate.service.AccumulateService;
import com.web.common.BaseResult;
import com.web.common.enums.BaseStatus;
import com.web.config.ExcelStylerUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Api(value = "API - AccumulateController",description = "accumulateTransaction")
@RestController
@CrossOrigin
@RequestMapping("/accumulate")
public class AccumulateController {

    @Autowired
    private AccumulateService accumulateService;

    @RequestMapping(value = "/timedTask")
    public void timedTask(){
        accumulateService.accumulateTimedTask();
    }

    @RequestMapping(value = "/addAccumulateTransaction",method = RequestMethod.POST)
    @ApiOperation(value = "新增accumulateTransaction")
    public BaseResult addAccumulateTransaction(@RequestBody AccumulateTransaction accumulateTransaction){
    	if(accumulateService.addAccumulateTransaction(accumulateTransaction) <= 0){
            return new BaseResult<>(BaseStatus.FCE_ACCUMULATED_FAIL.getCode(),BaseStatus.FCE_ACCUMULATED_FAIL.getDesc());
        }
        return new BaseResult<>(BaseStatus.SUCCESS.getCode(),BaseStatus.FCE_INSERT_SUCCESS.getDesc());
    }

    @RequestMapping(value = "/accumulateList")
    public BaseResult<List<AccumulateTransaction>> accumulateList(@ModelAttribute AccumulateTransaction accumulateTransaction){
    	List<AccumulateTransaction> list = accumulateService.getAccumulateTransactions(accumulateTransaction);
    	return new BaseResult<>(BaseStatus.SUCCESS.getCode(),list);
    }


    @RequestMapping(value = "/export")
    public void exportPosition(@ModelAttribute AccumulateTransaction accumulateTransaction, HttpServletResponse response) throws IOException {
        List<AccumulateTransaction> transactionList = accumulateService.getAccumulateTransactions(accumulateTransaction);

        String fileName = "累总交易数据";
        String sheetName = "Accumulate交易数据";
        ExportParams exportParams = new ExportParams(fileName,sheetName, ExcelType.XSSF);
        exportParams.setStyle(ExcelStylerUtil.class);
        Workbook workbook = ExcelExportUtil.exportExcel(exportParams, AccumulateTransaction.class, transactionList);

        //将表格输出
        fileName = URLEncoder.encode(fileName,"UTF8");
        //设置返回数据类型
        response.setContentType("application/vnd.ms-excel;charset=utf-8");
        response.setHeader("Content-Disposition", "attachment;filename=" + fileName + ".xls");
        workbook.write(response.getOutputStream());
    }
}
